/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.venta;

import Model.TiendaM;
import MsSQL.ConMsSQL;
import java.io.InputStream;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRSaveContributor;
import net.sf.jasperreports.view.JRViewer;

/**
 *
 * @author Maryluz
 */
public class frmMovimientoCaja extends javax.swing.JDialog {

    /**
     * Creates new form frmMovimientoCaja
     */
    int idT;
    TiendaM oMT;
    SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
    JDialog jDialogMostrar;
    Date oDate;
    private final String PDF = "PDF (*.pdf)";
    private final String DOC = "DOCX (*.docx)";
    private final String XLS = "Single sheet XLS (*.xls)";
    private final List<String> PERMITTED_FILE_TYPES_LIST = Arrays.asList(PDF, DOC, XLS);

    public frmMovimientoCaja(java.awt.Frame parent, boolean modal, int idTienda, TiendaM oM) {
        super(parent, modal);
        initComponents();
        idT = idTienda;
        oMT = oM;
        txtFecha.setFormats("dd/MM/yyyy");
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnIngreso = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnEgreso = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jToolBar2 = new javax.swing.JToolBar();
        btnCaja = new javax.swing.JButton();
        txtFecha = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Movimientos de Caja");

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(100, 100));

        btnIngreso.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnIngreso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/IngresoC.png"))); // NOI18N
        btnIngreso.setText("REGISTRAR INGRESOS");
        btnIngreso.setFocusable(false);
        btnIngreso.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnIngreso.setMaximumSize(new java.awt.Dimension(200, 200));
        btnIngreso.setPreferredSize(new java.awt.Dimension(100, 100));
        btnIngreso.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnIngreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnIngreso);

        jLabel1.setText("         ");
        jLabel1.setToolTipText("");
        jLabel1.setPreferredSize(new java.awt.Dimension(37, 14));
        jToolBar1.add(jLabel1);

        btnEgreso.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnEgreso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/EgresoC.png"))); // NOI18N
        btnEgreso.setText("REGISTRAR EGRESOS");
        btnEgreso.setFocusable(false);
        btnEgreso.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEgreso.setMaximumSize(new java.awt.Dimension(200, 200));
        btnEgreso.setPreferredSize(new java.awt.Dimension(100, 100));
        btnEgreso.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEgreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEgresoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEgreso);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel11.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setText("MOVIMIENTOS EN CAJA");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addContainerGap())
        );

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);
        jToolBar2.setMinimumSize(new java.awt.Dimension(482, 62));
        jToolBar2.setPreferredSize(new java.awt.Dimension(335, 62));

        btnCaja.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnCaja.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Money.png"))); // NOI18N
        btnCaja.setText("<HTML>CONSULTAR EFECTIVO DE CAJA DEL DÍA</HTML>");
        btnCaja.setFocusPainted(false);
        btnCaja.setFocusable(false);
        btnCaja.setMaximumSize(new java.awt.Dimension(2147483647, 65));
        btnCaja.setMinimumSize(new java.awt.Dimension(200, 30));
        btnCaja.setPreferredSize(new java.awt.Dimension(163, 45));
        btnCaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCajaActionPerformed(evt);
            }
        });
        jToolBar2.add(btnCaja);

        txtFecha.setFocusable(false);
        txtFecha.setPreferredSize(new java.awt.Dimension(103, 27));
        txtFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFechaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 19, Short.MAX_VALUE)
                        .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1))
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIngresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresoActionPerformed

        new frmIngreso(null, true, idT, oMT).setVisible(true);
        // this.dispose();

    }//GEN-LAST:event_btnIngresoActionPerformed

    private void btnEgresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEgresoActionPerformed

        new frmEgreso(null, true, idT, oMT).setVisible(true);
        //this.dispose();

    }//GEN-LAST:event_btnEgresoActionPerformed

    private void btnCajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCajaActionPerformed
        abrirReporteCaja(oMT.getNombres(), txtFecha.getDate());
    }//GEN-LAST:event_btnCajaActionPerformed

    private void txtFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFechaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtFechaActionPerformed
    @SuppressWarnings("CallToThreadDumpStack")
    private void abrirReporteCaja(String resp, Date fecha) {
        try {
            Connection conexion = ConMsSQL.obtenerConexion();
            ClassLoader cl = getClass().getClassLoader();
            InputStream in = cl.getClass().getResourceAsStream("/View/venta/reportes/rpt_caja.jasper");
            HashMap objHashMap = new HashMap();
            objHashMap.put("fecha", fecha);
            objHashMap.put("responsable", resp);
            objHashMap.put("idTienda", idT);
            JasperPrint objJasperPrint = JasperFillManager.fillReport(in, objHashMap, conexion);

            JRViewer jRViewer = new JRViewer(objJasperPrint);
            List<JRSaveContributor> SaveOptions = Arrays.asList(jRViewer.getSaveContributors());
            Iterator<JRSaveContributor> i = SaveOptions.iterator();

            while (i.hasNext()) {
                JRSaveContributor jsc = i.next();

                if (PERMITTED_FILE_TYPES_LIST.contains(jsc.getDescription())) {
                    continue;
                } else {
                    jRViewer.removeSaveContributor(jsc);
                }
            }

            jDialogMostrar = new JDialog(this, true);
            jDialogMostrar.setTitle("Reporte de Efectivo de Caja ");
            jDialogMostrar.setSize(350, 515);
            jDialogMostrar.setLocationRelativeTo(null);
            jDialogMostrar.add(jRViewer);
            jDialogMostrar.setVisible(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "No Existe Información para mostrar");
            // e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, e.toString());
            //            jDialogMostrar.setVisible(false);
            // e.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCaja;
    private javax.swing.JButton btnEgreso;
    private javax.swing.JButton btnIngreso;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private org.jdesktop.swingx.JXDatePicker txtFecha;
    // End of variables declaration//GEN-END:variables
}
